package cn.wen.image;

import android.app.Activity;
import android.app.LoaderManager;
import android.content.CursorLoader;
import android.content.Loader;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.MediaStore;

import java.io.File;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import cn.wen.image.bean.ImageBean;


/**
 * 加载本地图片
 */

class SelectImageHelper {
    private static final String[] PROJECTION = {
            MediaStore.Images.Media.DATA,// 路径
            MediaStore.Images.Media.DISPLAY_NAME,// 显示名称
            MediaStore.Images.Media.DATE_ADDED,// 时间
            MediaStore.Images.Media.MIME_TYPE,// 类型
            MediaStore.Images.Media.SIZE,// 大小
            MediaStore.Images.Media._ID,// ID
    };
    private Activity mActivity;
    private static final int ALL_IMAGE_ID = 0x0011;// 加载所有图片的ID

    private SelectImageHelper(Activity activity) {
        this.mActivity = activity;
    }

    public static SelectImageHelper with(Activity activity) {
        return new SelectImageHelper(activity);
    }

    public void loadAllImage(final OnImageLoadedListener loadedListener) {
        mActivity.getLoaderManager().initLoader(ALL_IMAGE_ID, null, new LoaderManager.LoaderCallbacks<Cursor>() {
            @Override
            public Loader<Cursor> onCreateLoader(int id, Bundle args) {
                String selection = PROJECTION[4] + " >102400 AND " + PROJECTION[3] + " =? OR " + PROJECTION[3] + " =? ";
                String[] selectionArgs = {"image/png", "image/jpeg"};
                String sortOrder = PROJECTION[2] + " DESC";// 按时间排序
                CursorLoader cursorLoader = new CursorLoader(mActivity, MediaStore.Images.Media.EXTERNAL_CONTENT_URI, PROJECTION,
                        selection, selectionArgs, sortOrder
                );
                return cursorLoader;
            }

            @Override
            public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
                List<ImageBean> images = new ArrayList<>();
                Map<String, List<ImageBean>> dirMap = new LinkedHashMap<>();
                // 1.将所有图片添加进去
                dirMap.put(MediaStore.Images.Media.EXTERNAL_CONTENT_URI.toString(), images);

                if (data != null && data.getColumnCount() > 0) {
                    ImageBean bean = null;
                    while (data.moveToNext()) {
                        bean = new ImageBean();
                        images.add(bean);
                        bean.path = data.getString(data.getColumnIndexOrThrow(PROJECTION[0]));
                        bean.name = data.getString(data.getColumnIndexOrThrow(PROJECTION[1]));
                        bean.dataTime = data.getString(data.getColumnIndexOrThrow(PROJECTION[2]));
                        bean.size = data.getLong(data.getColumnIndexOrThrow(PROJECTION[4]));
                        // 判断文件图片所在文件夹
                        File file = new File(bean.path);
                        File parentFile = file.getParentFile();
                        if (parentFile == null) {
                            continue;
                        }
                        String parentPath = parentFile.getAbsolutePath();
                        if (parentPath == null) {
                            continue;
                        }
                        // 2. 将文件夹的图片保存起来
                        String key = parentFile.getName();
                        List<ImageBean> list = dirMap.get(key);
                        if (list == null) {
                            list = new ArrayList<>();
                        }
                        bean.parentDir = parentPath;
                        bean.dirName = parentFile.getName();
                        list.add(bean);
                        dirMap.put(key, list);

                    }
                    bean = null;
                    if (loadedListener != null) {
                        loadedListener.onImageLoaded(images, dirMap);
                    }
                }
            }

            @Override
            public void onLoaderReset(Loader<Cursor> loader) {

            }
        });
    }

    public interface OnImageLoadedListener {
        void onImageLoaded(List<ImageBean> images, Map<String, List<ImageBean>> dirMap);
    }
}

